﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using HYElectricCalculation;

namespace YArchitech.Revit.MEP.Calculator
{
	public partial class LoadCalculationForm : System.Windows.Forms.Form
	{
		public LoadCalculationForm(ExternalCommandData m_commandData)
		{
			this.InitializeComponent();
			this.commandData = m_commandData;
		}

		private void LoadCalculationForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			this.panelLevel2Second.Dock = DockStyle.None;
			this.panelLevel2Second.Visible = false;
			this.panelLevel2Main.Dock = DockStyle.Fill;
			this.InitDataGridViewSource();
			this.nudCount.TextChanged += this.nudCount_TextChanged;
		}

		public void InitDataGridViewSource()
		{
			this.dgvLoadCal.RefreshEdit();
			this.dgvLoadCal.Rows.Clear();
			this.dgvLoadCal.Rows.Add(3);
			this.dgvLoadCal.Rows[this.dgvLoadCal.Rows.Count - 1].Cells[1].Value = "总计：";
			this.dgvLoadCal.Rows[this.dgvLoadCal.Rows.Count - 1].ReadOnly = true;
			this.dgvLoadCal.Rows[this.dgvLoadCal.Rows.Count - 1].DefaultCellStyle.BackColor = System.Drawing.Color.Wheat;
			this.dgvLoadCal.Rows[this.dgvLoadCal.RowCount - 2].DefaultCellStyle.BackColor = System.Drawing.Color.White;
			this.dgvLoadCal.Rows[this.dgvLoadCal.RowCount - 2].ReadOnly = true;
			this.dgvCalResult.Rows.Clear();
			this.dgvCalResult.Rows.Add();
			this.dgvCalResult.Rows[0].Cells[0].Value = 0.9;
			this.dgvCalResult.Rows[0].Cells[1].Value = 0.97;
			this.dgvCalCCResult.Rows.Clear();
			this.dgvCalCCResult.Rows.Add();
			this.dgvCalCCResult.Rows[0].Cells[0].Value = 0.7;
			this.dgvCalCCResult.Rows[0].Cells[1].Value = 0.76;
			this.dgvCalCCResult.Rows[0].Cells[2].Value = 0.9;
			if (this.cmbTransformName.Items.Count == 0)
			{
				this.transformerDatas = LoadCalculationHelper.GetTransformerData();
				foreach (TransformerData transformerData in this.transformerDatas)
				{
					this.cmbTransformName.Items.Add(transformerData.Name);
				}
			}
			this.cmbTransformName.SelectedIndex = 0;
			this.fullPath = "";
			this.isEdit = false;
			this.isFirst = true;
		}

		private void dgvLoadCal_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
		{
			if (this.isFirst && e.RowIndex == this.dgvLoadCal.Rows.Count - 2)
			{
				try
				{
					using (Brush brush = new SolidBrush(System.Drawing.Color.Gray))
					{
						using (Brush brush2 = new SolidBrush(e.CellStyle.BackColor))
						{
							using (Pen pen = new Pen(brush))
							{
								e.Graphics.FillRectangle(brush2, e.CellBounds);
								e.Graphics.DrawLine(pen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
								e.Handled = true;
							}
						}
					}
				}
				catch (Exception)
				{
				}
				this.isFirst = false;
			}
		}

		private void dgvLoadCal_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
		{
			this.dgvLoadCal.Rows[e.RowIndex].Cells[2].Value = "A,B,C";
			this.dgvLoadCal.Rows[e.RowIndex].Cells[3].Value = 0.38;
			this.dgvLoadCal.Rows[e.RowIndex].Cells[5].Value = 1.0;
			this.dgvLoadCal.Rows[e.RowIndex].Cells[6].Value = 0.8;
		}

		private void dgvLoadCal_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			if (0 <= this.dgvLoadCal.CurrentCell.RowIndex && this.dgvLoadCal.CurrentCell.RowIndex < this.dgvLoadCal.RowCount - 3)
			{
				e.Control.ContextMenuStrip = this.contextMenuStrip1;
				this.cmcopy.Enabled = true;
				this.cmCut.Enabled = true;
				this.cmdelete.Enabled = true;
			}
			else if (this.dgvLoadCal.CurrentCell.RowIndex == this.dgvLoadCal.RowCount - 3)
			{
				e.Control.ContextMenuStrip = this.contextMenuStrip1;
				this.cmcopy.Enabled = false;
				this.cmCut.Enabled = false;
				this.cmdelete.Enabled = false;
			}
			if (this.dgvLoadCal.CurrentCell.OwningColumn.Name == "Phase")
			{
				((System.Windows.Forms.ComboBox)e.Control).SelectedIndexChanged -= this.ComboBox_SelectedIndexChanged;
				((System.Windows.Forms.ComboBox)e.Control).SelectedIndexChanged += this.ComboBox_SelectedIndexChanged;
				return;
			}
			if (this.dgvLoadCal.CurrentCell.OwningColumn.Name == "Voltage" || this.dgvLoadCal.CurrentCell.OwningColumn.Name == "Power" || this.dgvLoadCal.CurrentCell.OwningColumn.Name == "NeedFactor" || this.dgvLoadCal.CurrentCell.OwningColumn.Name == "PowerFactor")
			{
				e.Control.KeyPress -= this.TextBoxDec_KeyPress;
				e.Control.KeyPress += this.TextBoxDec_KeyPress;
				return;
			}
			e.Control.KeyPress -= this.TextBoxDec_KeyPress;
		}

		private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			if ((sender as System.Windows.Forms.ComboBox).SelectedItem.ToString() == "A,B,C")
			{
				this.dgvLoadCal.Rows[this.dgvLoadCal.CurrentCell.RowIndex].Cells["Phase"].Value = "A,B,C";
				this.dgvLoadCal.Rows[this.dgvLoadCal.CurrentCell.RowIndex].Cells["Voltage"].Value = 0.38;
			}
			else
			{
				this.dgvLoadCal.Rows[this.dgvLoadCal.CurrentCell.RowIndex].Cells["Phase"].Value = "A";
				this.dgvLoadCal.Rows[this.dgvLoadCal.CurrentCell.RowIndex].Cells["Voltage"].Value = 0.22;
			}
			this.loadCal(this.dgvLoadCal.CurrentCell.RowIndex);
			this.CalResult();
			this.CalCCResult();
			this.CalTransformer();
			this.isEdit = true;
		}

		private void TextBoxDec_KeyPress(object sender, KeyPressEventArgs e)
		{
			System.Windows.Forms.TextBox textBox = sender as System.Windows.Forms.TextBox;
			if (!string.IsNullOrEmpty(textBox.SelectedText) && this.dgvLoadCal.SelectedCells.Count == 11 && e.KeyChar == '\u0003')
			{
				Clipboard.SetText((this.dgvLoadCal.CurrentCell.Value == DBNull.Value) ? " " : this.dgvLoadCal.CurrentCell.Value.ToString().Trim());
			}
			if (this.dgvLoadCal.CurrentCell.OwningColumn.Name == "Voltage")
			{
				e.Handled = true;
				return;
			}
			if (e.KeyChar == '\u0016')
			{
				try
				{
					double num;
					if (double.TryParse(Clipboard.GetText(), out num))
					{
						this.dgvLoadCal.CurrentCell.Value = num;
					}
					else
					{
						e.Handled = true;
					}
					goto IL_EC;
				}
				catch (Exception)
				{
					e.Handled = true;
					goto IL_EC;
				}
			}
			this.Validata(textBox.Text.Trim(), e);
			IL_EC:
			this.isEdit = true;
		}

		private void Validata(string txt, KeyPressEventArgs e)
		{
			if (e.KeyChar == ' ')
			{
				e.Handled = true;
				return;
			}
			if (e.KeyChar == '\b')
			{
				return;
			}
			double num = 0.0;
			if (!double.TryParse(txt + e.KeyChar.ToString(), out num))
			{
				e.Handled = true;
				return;
			}
			if (num >= 0.0)
			{
				e.Handled = false;
				return;
			}
			e.Handled = true;
		}

		private void dgvLoadCal_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
		{
			if (this.dgvLoadCal.Columns[e.ColumnIndex].Name == "PowerFactor" && this.dgvLoadCal.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
			{
				this.editPowerFactor = this.dgvLoadCal.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
			}
		}

		private void dgvLoadCal_CellEndEdit(object sender, DataGridViewCellEventArgs e)
		{
			if (this.isProjectAdd)
			{
				this.isProjectAdd = false;
				return;
			}
			if (this.dgvLoadCal.Columns[e.ColumnIndex].Name == "PowerFactor" || this.dgvLoadCal.Columns[e.ColumnIndex].Name == "Number" || this.dgvLoadCal.Columns[e.ColumnIndex].Name == "DeviceGroupName" || this.dgvLoadCal.Columns[e.ColumnIndex].Name == "Power" || this.dgvLoadCal.Columns[e.ColumnIndex].Name == "NeedFactor" || this.dgvLoadCal.Columns[e.ColumnIndex].Name == "Phase")
			{
				object value = this.dgvLoadCal.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
				if (this.dgvLoadCal.Columns[e.ColumnIndex].Name == "PowerFactor" && (Convert.ToDouble(value) <= 0.0 || Convert.ToDouble(value) > 1.0))
				{
					MessageBox.Show("\"功率因数\"的取值范围为0到1之间,请修改。", "盈建科提示");
					this.editRowIndex = e.RowIndex;
					this.editColIndex = e.ColumnIndex;
					this.dgvLoadCal.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = this.editPowerFactor;
					return;
				}
				if (this.dgvLoadCal.Columns[e.ColumnIndex].Name == "Number" || this.dgvLoadCal.Columns[e.ColumnIndex].Name == "DeviceGroupName" || this.dgvLoadCal.Columns[e.ColumnIndex].Name == "Power")
				{
					if (value == null)
					{
						if (this.dgvLoadCal.Rows.Count - 3 != e.RowIndex)
						{
							this.loadCal(e.RowIndex);
							this.CalResult();
							this.CalCCResult();
							this.CalTransformer();
						}
						return;
					}
					if (e.RowIndex == this.dgvLoadCal.Rows.Count - 3)
					{
						this.dgvLoadCal.Rows.Insert(this.dgvLoadCal.Rows.Count - 2, Array.Empty<object>());
					}
				}
				if ((this.dgvLoadCal.Columns[e.ColumnIndex].Name == "Power" || this.dgvLoadCal.Columns[e.ColumnIndex].Name == "NeedFactor" || this.dgvLoadCal.Columns[e.ColumnIndex].Name == "PowerFactor" || this.dgvLoadCal.Columns[e.ColumnIndex].Name == "Phase") && this.dgvLoadCal.Rows.Count - 3 != e.RowIndex)
				{
					this.loadCal(e.RowIndex);
					this.CalResult();
					this.CalCCResult();
					this.CalTransformer();
				}
			}
		}

		private void dgvLoadCal_SelectionChanged(object sender, EventArgs e)
		{
			if (this.editRowIndex != -1 && this.editColIndex != -1)
			{
				int index = this.editRowIndex;
				int index2 = this.editColIndex;
				this.editRowIndex = -1;
				this.editColIndex = -1;
				this.dgvLoadCal.Rows[index].Cells[index2].Selected = true;
			}
			if (this.dgvLoadCal.SelectedRows.Count > 0 && this.dgvLoadCal.SelectedRows[0] == this.dgvLoadCal.Rows[this.dgvLoadCal.RowCount - 2])
			{
				this.dgvLoadCal.Rows[this.dgvLoadCal.RowCount - 3].Selected = true;
			}
		}

		private void dgvCalResult_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			if (this.dgvCalResult.CurrentCell.ColumnIndex == 0 || this.dgvCalResult.CurrentCell.ColumnIndex == 1)
			{
				e.Control.KeyPress -= this.TextBoxEx_KeyPress;
				e.Control.KeyPress += this.TextBoxEx_KeyPress;
				return;
			}
			e.Control.KeyPress -= this.TextBoxEx_KeyPress;
		}

		private void TextBoxEx_KeyPress(object sender, KeyPressEventArgs e)
		{
			System.Windows.Forms.TextBox textBox = sender as System.Windows.Forms.TextBox;
			this.Validata(textBox.Text.Trim(), e);
			this.isEdit = true;
		}

		private void dgvCalResult_CellEndEdit(object sender, DataGridViewCellEventArgs e)
		{
			if (this.dgvCalResult.CurrentCell.ColumnIndex == 0 || this.dgvCalResult.CurrentCell.ColumnIndex == 1)
			{
				this.CalResult();
				this.CalCCResult();
				this.CalTransformer();
			}
		}

		private void dgvCalCCResult_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			if (this.dgvCalCCResult.CurrentCell.ColumnIndex == 0 || this.dgvCalCCResult.CurrentCell.ColumnIndex == 1 || this.dgvCalCCResult.CurrentCell.ColumnIndex == 2 || this.dgvCalCCResult.CurrentCell.ColumnIndex == 4)
			{
				e.Control.KeyPress -= this.TextBoxEx_KeyPress;
				e.Control.KeyPress += this.TextBoxEx_KeyPress;
				return;
			}
			e.Control.KeyPress -= this.TextBoxEx_KeyPress;
		}

		private void dgvCalCCResult_CellEndEdit(object sender, DataGridViewCellEventArgs e)
		{
			if (this.dgvCalCCResult.CurrentCell.ColumnIndex == 0 || this.dgvCalCCResult.CurrentCell.ColumnIndex == 1 || this.dgvCalCCResult.CurrentCell.ColumnIndex == 2 || this.dgvCalCCResult.CurrentCell.ColumnIndex == 4)
			{
				this.CalCCResult();
				this.CalTransformer();
			}
		}

		private void cmbTransformName_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.cmbTransformSpec.Items.Clear();
			foreach (TransformerData transformerData in this.transformerDatas)
			{
				if (transformerData.Name == this.cmbTransformName.SelectedItem.ToString())
				{
					foreach (string item in transformerData.Specs.Keys)
					{
						this.cmbTransformSpec.Items.Add(item);
					}
				}
			}
			if (this.cmbTransformSpec.Items.Count > 0)
			{
				this.CalTransformer();
			}
		}

		private void tbLoadRate_KeyPress(object sender, KeyPressEventArgs e)
		{
			this.Validata(this.tbLoadRate.Text, e);
		}

		private void tbLoadRate_TextChanged(object sender, EventArgs e)
		{
			if (this.tbLoadRate.Text.Length > 0)
			{
				this.CalTransformer();
				return;
			}
			MessageBox.Show("负荷率不能为空！", "盈建科提示");
			this.tbLoadRate.Text = "0.8";
			this.tbLoadRate.SelectAll();
		}

		private void nudCount_TextChanged(object sender, EventArgs e)
		{
			System.Windows.Forms.Control control = sender as Control;
			if (string.IsNullOrEmpty(control.Text))
			{
				MessageBox.Show("变压器台数不能为空！", "盈建科提示");
				control.Text = "1";
			}
			int num;
			if (int.TryParse(this.nudCount.Value.ToString().Trim(), out num) && this.nudCount.Value >= 1m)
			{
				this.CalTransformer();
			}
		}

		public void loadCal(int rowIndex)
		{
			DataGridViewRow dataGridViewRow = this.dgvLoadCal.Rows[rowIndex];
			double num = LoadCalculationHelper.Cal.CalActivePower(Convert.ToDouble(dataGridViewRow.Cells["Power"].Value), Convert.ToDouble(dataGridViewRow.Cells["NeedFactor"].Value));
			double num2 = LoadCalculationHelper.Cal.CalRectivePower(num, Convert.ToDouble(dataGridViewRow.Cells["PowerFactor"].Value));
			double apparentPower = LoadCalculationHelper.Cal.GetApparentPower(num, num2);
			double value;
			if (dataGridViewRow.Cells["Phase"].Value.ToString() == "A,B,C")
			{
				value = LoadCalculationHelper.Cal.CalElectricCurrent3(apparentPower);
			}
			else
			{
				value = LoadCalculationHelper.Cal.CalElectricCurrent(apparentPower);
			}
			dataGridViewRow.Cells["ActivePower"].Value = LoadCalculationHelper.Cal.ReservedDecimal(num);
			dataGridViewRow.Cells["ReactivePower"].Value = LoadCalculationHelper.Cal.ReservedDecimal(num2);
			dataGridViewRow.Cells["ApparentPower"].Value = LoadCalculationHelper.Cal.ReservedDecimal(apparentPower);
			dataGridViewRow.Cells["CalElectricCurrent"].Value = LoadCalculationHelper.Cal.ReservedDecimal(value);
			DataGridViewRow dataGridViewRow2 = this.dgvLoadCal.Rows[this.dgvLoadCal.Rows.Count - 1];
			double num3 = this.SumData("ActivePower");
			double num4 = this.SumData("ReactivePower");
			double apparentPower2 = LoadCalculationHelper.Cal.GetApparentPower(num3, num4);
			double value2 = LoadCalculationHelper.Cal.CalElectricCurrent3(apparentPower2);
			dataGridViewRow2.Cells[4].Value = this.SumColumnsVale("Power");
			dataGridViewRow2.Cells[7].Value = LoadCalculationHelper.Cal.ReservedDecimal(num3);
			dataGridViewRow2.Cells[8].Value = LoadCalculationHelper.Cal.ReservedDecimal(num4);
			dataGridViewRow2.Cells[9].Value = LoadCalculationHelper.Cal.ReservedDecimal(apparentPower2);
			dataGridViewRow2.Cells[10].Value = LoadCalculationHelper.Cal.ReservedDecimal(value2);
		}

		public double SumData(string type)
		{
			double num = 0.0;
			double num2 = 0.0;
			double num3 = 0.0;
			double num4 = 0.0;
			for (int i = 0; i < this.dgvLoadCal.Rows.Count - 2; i++)
			{
				DataGridViewCell dataGridViewCell = this.dgvLoadCal.Rows[i].Cells["Phase"];
				DataGridViewCell dataGridViewCell2 = this.dgvLoadCal.Rows[i].Cells[type];
				if (dataGridViewCell2.Value != null)
				{
					if (dataGridViewCell.Value.ToString() == "A")
					{
						num += Convert.ToDouble(dataGridViewCell2.Value);
					}
					else if (dataGridViewCell.Value.ToString() == "B")
					{
						num2 += Convert.ToDouble(dataGridViewCell2.Value);
					}
					else if (dataGridViewCell.Value.ToString() == "C")
					{
						num3 += Convert.ToDouble(dataGridViewCell2.Value);
					}
					else
					{
						num4 += Convert.ToDouble(dataGridViewCell2.Value);
					}
				}
			}
			double num5;
			if (num >= num2)
			{
				num5 = num;
			}
			else
			{
				num5 = num2;
			}
			if (num5 <= num3)
			{
				num5 = num3;
			}
			return num5 * 3.0 + num4;
		}

		public double SumColumnsVale(string power)
		{
			double num = 0.0;
			for (int i = 0; i < this.dgvLoadCal.Rows.Count - 2; i++)
			{
				if (this.dgvLoadCal.Rows[i].Cells[power].Value != null)
				{
					num += Convert.ToDouble(this.dgvLoadCal.Rows[i].Cells[power].Value);
				}
			}
			return num;
		}

		public void CalResult()
		{
			this.CalTotalActivePower();
			this.CalTotalApparentPower();
			double apparentPower = LoadCalculationHelper.Cal.GetApparentPower(Convert.ToDouble(this.dgvCalResult.Rows[0].Cells[3].Value), Convert.ToDouble(this.dgvCalResult.Rows[0].Cells[4].Value));
			this.dgvCalResult.Rows[0].Cells[5].Value = LoadCalculationHelper.Cal.ReservedDecimal(apparentPower);
			double num = LoadCalculationHelper.Cal.CalTotolPowerFactor(Convert.ToDouble(this.dgvCalResult.Rows[0].Cells[4].Value), Convert.ToDouble(this.dgvCalResult.Rows[0].Cells[3].Value));
			if (double.IsNaN(num) || double.IsInfinity(num))
			{
				this.dgvCalResult.Rows[0].Cells[2].Value = LoadCalculationHelper.Cal.ReservedDecimal(0.0);
			}
			else
			{
				this.dgvCalResult.Rows[0].Cells[2].Value = LoadCalculationHelper.Cal.ReservedDecimal(num);
			}
			double value = LoadCalculationHelper.Cal.CalElectricCurrent3(apparentPower);
			this.dgvCalResult.Rows[0].Cells[6].Value = LoadCalculationHelper.Cal.ReservedDecimal(value);
		}

		public void CalTotalActivePower()
		{
			double value = Convert.ToDouble(this.dgvCalResult.Rows[0].Cells[0].Value) * Convert.ToDouble(this.dgvLoadCal.Rows[this.dgvLoadCal.Rows.Count - 1].Cells[7].Value);
			this.dgvCalResult.Rows[0].Cells[3].Value = LoadCalculationHelper.Cal.ReservedDecimal(value);
		}

		public void CalTotalApparentPower()
		{
			double value = Convert.ToDouble(this.dgvCalResult.Rows[0].Cells[1].Value) * Convert.ToDouble(this.dgvLoadCal.Rows[this.dgvLoadCal.Rows.Count - 1].Cells[8].Value);
			this.dgvCalResult.Rows[0].Cells[4].Value = LoadCalculationHelper.Cal.ReservedDecimal(value);
		}

		public void CalCCResult()
		{
			this.CCCal();
			if (this.dgvCalResult.Rows[0].Cells[4].Value != null)
			{
				double num = 0.0;
				double activePower = Convert.ToDouble(this.dgvCalResult.Rows[0].Cells[3].Value);
				double num2 = Convert.ToDouble(this.dgvCalResult.Rows[0].Cells[4].Value);
				if (this.dgvCalCCResult.Rows[0].Cells[4].Value != null && this.dgvCalCCResult.Rows[0].Cells[4].Value.ToString() != "")
				{
					num = Convert.ToDouble(this.dgvCalCCResult.Rows[0].Cells[4].Value);
				}
				double num3;
				if (this.dgvCalCCResult.Rows[0].Cells[4].Value == null || this.dgvCalCCResult.Rows[0].Cells[4].Value.ToString() == "")
				{
					num3 = num2 - Convert.ToDouble(this.dgvCalCCResult.Rows[0].Cells[3].Value);
				}
				else
				{
					num3 = num2 - num;
				}
				if (double.IsNaN(num3) || double.IsInfinity(num3))
				{
					this.dgvCalCCResult.Rows[0].Cells[5].Value = LoadCalculationHelper.Cal.ReservedDecimal(0.0);
				}
				else
				{
					this.dgvCalCCResult.Rows[0].Cells[5].Value = LoadCalculationHelper.Cal.ReservedDecimal(num3);
				}
				if (double.IsNaN(LoadCalculationHelper.Cal.GetApparentPower(activePower, num3)) || double.IsInfinity(LoadCalculationHelper.Cal.GetApparentPower(activePower, num3)))
				{
					this.dgvCalCCResult.Rows[0].Cells[6].Value = LoadCalculationHelper.Cal.ReservedDecimal(0.0);
					return;
				}
				this.dgvCalCCResult.Rows[0].Cells[6].Value = LoadCalculationHelper.Cal.ReservedDecimal(LoadCalculationHelper.Cal.GetApparentPower(activePower, num3));
			}
		}

		public void CCCal()
		{
			double activePower = Convert.ToDouble(this.dgvCalResult.Rows[0].Cells[3].Value);
			double reActivePower = Convert.ToDouble(this.dgvCalResult.Rows[0].Cells[4].Value);
			double num = Convert.ToDouble(this.dgvCalCCResult.Rows[0].Cells[0].Value);
			double reactiveLoadFactor = Convert.ToDouble(this.dgvCalCCResult.Rows[0].Cells[1].Value);
			double cos = LoadCalculationHelper.Cal.CalculatComFator(activePower, reActivePower, num, reactiveLoadFactor);
			double num2 = LoadCalculationHelper.Cal.CalCC(num, activePower, LoadCalculationHelper.Cal.CosTransformTan(cos), LoadCalculationHelper.Cal.CosTransformTan(Convert.ToDouble(this.dgvCalCCResult.Rows[0].Cells[2].Value)));
			if (double.IsNaN(num2) || double.IsInfinity(num2))
			{
				this.dgvCalCCResult.Rows[0].Cells[3].Value = LoadCalculationHelper.Cal.ReservedDecimal(0.0);
				return;
			}
			this.dgvCalCCResult.Rows[0].Cells[3].Value = DataCalculationAndHelper.MathDecimal(num2, 2);
		}

		public void CalTransformer()
		{
			double num = Convert.ToDouble(this.dgvCalCCResult.Rows[0].Cells[6].Value) / Convert.ToDouble(this.tbLoadRate.Text.Trim());
			num /= Convert.ToDouble(this.nudCount.Value);
			num = Math.Round(num, 2);
			Dictionary<string, double> dictionary = new Dictionary<string, double>();
			Dictionary<string, double> dictionary2 = new Dictionary<string, double>();
			foreach (TransformerData transformerData in this.transformerDatas)
			{
				if (transformerData.Name == this.cmbTransformName.SelectedItem.ToString())
				{
					using (Dictionary<string, double>.KeyCollection.Enumerator enumerator2 = transformerData.Specs.Keys.GetEnumerator())
					{
						while (enumerator2.MoveNext())
						{
							string key = enumerator2.Current;
							dictionary.Add(key, transformerData.Specs[key] / 1000.0);
							if (transformerData.Specs[key] / 1000.0 >= num)
							{
								dictionary2.Add(key, transformerData.Specs[key] / 1000.0);
							}
						}
						break;
					}
				}
			}
			if (dictionary2.Count > 0)
			{
				Dictionary<string, double> dictionary3 = (from p in dictionary2
				orderby p.Value
				select p).ToDictionary((KeyValuePair<string, double> p) => p.Key, (KeyValuePair<string, double> p) => p.Value);
				this.cmbTransformSpec.SelectedItem = dictionary3.Keys.First<string>();
				return;
			}
			Dictionary<string, double> dictionary4 = (from l in dictionary
			orderby l.Value
			select l).ToDictionary((KeyValuePair<string, double> l) => l.Key, (KeyValuePair<string, double> l) => l.Value);
			this.cmbTransformSpec.SelectedItem = dictionary4.Keys.Last<string>();
		}

		private void tsbCreate_Click(object sender, EventArgs e)
		{
			if (this.isEdit)
			{
				MessageBoxButtons buttons = MessageBoxButtons.YesNo;
				if (MessageBox.Show("是否保存当前数据？", "盈建科提示", buttons) == DialogResult.Yes)
				{
					this.SaveXml();
				}
			}
			this.InitDataGridViewSource();
		}

		private void tsbProjectAdd_Click(object sender, EventArgs e)
		{
			if (this.isEdit)
			{
				MessageBoxButtons buttons = MessageBoxButtons.YesNo;
				if (MessageBox.Show("是否保存当前数据？", "盈建科提示", buttons) == DialogResult.Yes)
				{
					this.SaveXml();
				}
			}
			this.InitDataGridViewSource();
			List<LoadCalculationData> list = null;
			for (int i = 0; i < list.Count; i++)
			{
				this.dgvLoadCal.Rows.Insert(i, Array.Empty<object>());
				this.dgvLoadCal.Rows[i].Cells[0].Value = list[i].Number;
				this.dgvLoadCal.Rows[i].Cells[1].Value = list[i].DeviceGroupName;
				this.dgvLoadCal.Rows[i].Cells[2].Value = ((list[i].Phase == "3") ? "A,B,C" : "A");
				this.dgvLoadCal.Rows[i].Cells[3].Value = ((list[i].Phase == "3") ? 0.35 : 0.22);
				this.dgvLoadCal.Rows[i].Cells[4].Value = list[i].Power;
				this.dgvLoadCal.Rows[i].Cells[5].Value = list[i].NeedFactor;
				this.dgvLoadCal.Rows[i].Cells[6].Value = list[i].PowerFactor;
			}
			for (int j = 0; j < this.dgvLoadCal.Rows.Count - 3; j++)
			{
				this.loadCal(j);
			}
			this.CalResult();
			this.CalCCResult();
			this.CalTransformer();
			this.isProjectAdd = true;
		}

		private void tsbLoad_Click(object sender, EventArgs e)
		{
			if (this.isEdit)
			{
				MessageBoxButtons buttons = MessageBoxButtons.YesNo;
				if (MessageBox.Show("是否保存当前数据？", "盈建科提示", buttons) == DialogResult.Yes)
				{
					this.SaveXml();
				}
			}
			this.openFileDialog1.Filter = "XLH文件(*.xlh)|*.xlh";
			this.openFileDialog1.FileName = "";
			if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
			{
				try
				{
					string fileName = this.openFileDialog1.FileName;
					this.fullPath = fileName;
					DataSet dataSet = new DataSet();
					dataSet.ReadXml(fileName);
					if (dataSet.Tables.Count > 0)
					{
						this.InitDataGridViewSource();
						DataTable dt = dataSet.Tables[0];
						this.ConvertToDataGridView(this.dgvLoadCal, dt);
					}
					goto IL_B6;
				}
				catch (Exception)
				{
					MessageBox.Show("读取外部负荷计算文件出错。", "盈建科提示");
					return;
				}
				return;
				IL_B6:
				if (this.dgvLoadCal.RowCount > 3)
				{
					for (int i = 0; i < this.dgvLoadCal.Rows.Count - 3; i++)
					{
						this.loadCal(i);
					}
					this.CalResult();
					this.CalCCResult();
					this.CalTransformer();
				}
				return;
			}
		}

		private void tsbSave_Click(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(this.fullPath))
			{
				this.SaveXml();
			}
			else
			{
				System.Data.DataTable dataTable = this.ConvertToTable(this.dgvLoadCal);
				dataTable.TableName = "Calculation";
				new DataSet("LoadCalculation")
				{
					Tables = 
					{
						dataTable
					}
				}.WriteXml(this.fullPath);
				MessageBox.Show("数据成功保存到" + this.saveFileDialog1.FileName, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
			}
			this.isEdit = false;
		}

		private void tsbSaveAs_Click(object sender, EventArgs e)
		{
			this.SaveXml();
			this.isEdit = false;
		}

		private void tsbExportExcel_Click(object sender, EventArgs e)
		{
			LoadCalculationHelper.ExportExcel(this);
		}

		private void tsbExportWord_Click(object sender, EventArgs e)
		{
			LoadCalculationHelper.ExportWord(this);
		}

		private void tsbTransformSelect_Click(object sender, EventArgs e)
		{
			if (this.isShow)
			{
				this.panelLevel2Main.Dock = DockStyle.Top;
				this.panelLevel2Main.Anchor = (AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right);
				this.panelLevel2Main.Width = base.ClientSize.Width;
				this.panelLevel2Main.Height = base.ClientRectangle.Height - 60;
				this.panelLevel2Second.Visible = true;
				this.panelLevel2Second.Dock = DockStyle.Bottom;
				this.isShow = false;
				return;
			}
			this.panelLevel2Second.Dock = DockStyle.None;
			this.panelLevel2Second.Visible = false;
			this.panelLevel2Main.Dock = DockStyle.Fill;
			this.isShow = true;
		}

		private void tsbExsit_Click(object sender, EventArgs e)
		{
			if (this.isEdit)
			{
				MessageBoxButtons buttons = MessageBoxButtons.YesNo;
				if (MessageBox.Show("是否保存当前数据？", "盈建科提示", buttons) == DialogResult.Yes)
				{
					this.SaveXml();
				}
				this.isEdit = false;
			}
			base.Close();
		}

		public void SaveXml()
		{
			string fileName = this.saveFileDialog1.FileName;
			this.saveFileDialog1.FileName = DateTime.Now.ToString("负荷计算");
			this.saveFileDialog1.Filter = "XLH文件(*.xlh)|*.xlh";
			if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
			{
				System.Data.DataTable dataTable = this.ConvertToTable(this.dgvLoadCal);
				dataTable.TableName = "Calculation";
				new DataSet("LoadCalculation")
				{
					Tables = 
					{
						dataTable
					}
				}.WriteXml(this.saveFileDialog1.FileName);
				this.fullPath = this.saveFileDialog1.FileName;
				MessageBox.Show("数据成功保存到" + this.saveFileDialog1.FileName, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
			}
		}

		public void ConvertToDataGridView(DataGridView dgvLoadCal, DataTable dt)
		{
			dgvLoadCal.RefreshEdit();
			for (int i = 0; i < dt.Rows.Count; i++)
			{
				dgvLoadCal.Rows.Insert(i, Array.Empty<object>());
				for (int j = 0; j < dt.Columns.Count; j++)
				{
					if (dt.Rows[i][j].ToString() == "")
					{
						dgvLoadCal.Rows[i].Cells[j].Value = null;
					}
					else
					{
						dgvLoadCal.Rows[i].Cells[j].Value = dt.Rows[i][j].ToString();
					}
				}
			}
		}

		public DataTable ConvertToTable(DataGridView dgv)
		{
			System.Data.DataTable dataTable = new DataTable();
			for (int i = 0; i < dgv.Columns.Count - 4; i++)
			{
				DataColumn column = new DataColumn(dgv.Columns[i].Name.ToString());
				dataTable.Columns.Add(column);
			}
			for (int j = 0; j < dgv.Rows.Count - 3; j++)
			{
				DataRow dataRow = dataTable.NewRow();
				for (int k = 0; k < dgv.Columns.Count - 4; k++)
				{
					if (dgv.Rows[j].Cells[k].Value == null)
					{
						dataRow[k] = "";
					}
					else
					{
						dataRow[k] = dgv.Rows[j].Cells[k].Value.ToString();
					}
				}
				dataTable.Rows.Add(dataRow);
			}
			return dataTable;
		}

		private void dgvLoadCal_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
		{
			if (e.Button != MouseButtons.Right || e.RowIndex >= this.dgvLoadCal.RowCount - 2)
			{
				return;
			}
			if (e.RowIndex == this.dgvLoadCal.RowCount - 3)
			{
				this.cmcopy.Enabled = false;
				this.cmCut.Enabled = false;
				this.cmdelete.Enabled = false;
			}
			else
			{
				this.cmcopy.Enabled = true;
				this.cmCut.Enabled = true;
				this.cmdelete.Enabled = true;
			}
			int num = 0;
			int num2 = 0;
			foreach (object obj in this.dgvLoadCal.Columns)
			{
				DataGridViewColumn dataGridViewColumn = (DataGridViewColumn)obj;
				if (dataGridViewColumn.Index >= e.ColumnIndex)
				{
					break;
				}
				if (dataGridViewColumn.Visible)
				{
					num += dataGridViewColumn.Width;
				}
			}
			foreach (object obj2 in ((IEnumerable)this.dgvLoadCal.Rows))
			{
				DataGridViewRow dataGridViewRow = (DataGridViewRow)obj2;
				if (dataGridViewRow.Index > e.RowIndex)
				{
					break;
				}
				if (dataGridViewRow.Visible)
				{
					num2 += dataGridViewRow.Height;
				}
			}
			if (e.RowIndex == this.dgvLoadCal.CurrentCell.RowIndex)
			{
				this.contextMenuStrip1.Show(this.dgvLoadCal.PointToScreen(new System.Drawing.Point(num + e.X, num2 + e.Y)));
			}
		}

		private void cmcopy_Click(object sender, EventArgs e)
		{
			if (this.dgvLoadCal.RowCount > 3 && this.dgvLoadCal.CurrentCell.RowIndex < this.dgvLoadCal.RowCount - 3)
			{
				this.copyDgvRowValue();
			}
		}

		private void cmCut_Click(object sender, EventArgs e)
		{
			if (this.dgvLoadCal.RowCount > 3 && this.dgvLoadCal.CurrentCell.RowIndex < this.dgvLoadCal.RowCount - 3)
			{
				this.copyDgvRowValue();
				this.dgvLoadCal.Rows.Remove(this.dgvLoadCal.Rows[this.dgvLoadCal.CurrentCell.RowIndex]);
				this.loadCal(this.dgvLoadCal.CurrentCell.RowIndex);
				this.CalResult();
				this.CalCCResult();
				this.CalTransformer();
			}
		}

		private void cmpast_Click(object sender, EventArgs e)
		{
			int num;
			if (this.dgvLoadCal.CurrentCell.RowIndex >= this.dgvLoadCal.RowCount - 3)
			{
				num = this.dgvLoadCal.RowCount - 3;
			}
			else
			{
				num = this.dgvLoadCal.CurrentCell.RowIndex + 1;
			}
			this.dgvLoadCal.Rows.Insert(num, new DataGridViewRow());
			for (int i = 0; i < this.dgvLoadCal.Columns.Count; i++)
			{
				this.dgvLoadCal.Rows[num].Cells[i].Value = this.dgvRowValue[i];
			}
			this.loadCal(num);
			this.CalResult();
			this.CalCCResult();
			this.CalTransformer();
		}

		private void cmdelete_Click(object sender, EventArgs e)
		{
			try
			{
				if (this.dgvLoadCal.Rows.Count > 3 && this.dgvLoadCal.CurrentCell.RowIndex != this.dgvLoadCal.Rows.Count - 2)
				{
					this.dgvLoadCal.Rows.Remove(this.dgvLoadCal.Rows[this.dgvLoadCal.CurrentCell.RowIndex]);
				}
			}
			catch (Exception)
			{
			}
			this.dgvLoadCal.Refresh();
			this.loadCal(0);
			this.CalResult();
			this.CalCCResult();
			this.CalTransformer();
		}

		public void copyDgvRowValue()
		{
			this.dgvRowValue.Clear();
			for (int i = 0; i < this.dgvLoadCal.Columns.Count; i++)
			{
				if (this.dgvLoadCal.Rows[this.dgvLoadCal.CurrentCell.RowIndex].Cells[i].Value == null)
				{
					this.dgvRowValue.Add(null);
				}
				else
				{
					this.dgvRowValue.Add(this.dgvLoadCal.Rows[this.dgvLoadCal.CurrentCell.RowIndex].Cells[i].Value.ToString());
				}
			}
			this.cmpast.Enabled = true;
		}

		private void dgvLoadCal_SizeChanged(object sender, EventArgs e)
		{
			if (this.percents.Count == this.dgvLoadCal.ColumnCount)
			{
				this.isSizeChanged = true;
				double num = (double)this.dgvLoadCal.Width;
				double num2 = 0.0;
				for (int i = 0; i < this.dgvLoadCal.ColumnCount - 1; i++)
				{
					this.dgvLoadCal.Columns[i].Width = (int)(this.percents[i] * num);
					num2 += (double)((int)(this.percents[i] * num));
				}
				this.dgvLoadCal.Columns[this.dgvLoadCal.ColumnCount - 1].Width = (int)(num - num2);
				this.isSizeChanged = false;
			}
		}

		protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
		{
			int num = 256;
			int num2 = 260;
			if ((msg.Msg == num | msg.Msg == num2) && keyData == Keys.Escape)
			{
				if (this.isEdit)
				{
					MessageBoxButtons buttons = MessageBoxButtons.YesNo;
					if (MessageBox.Show("是否保存当前数据？", "盈建科提示", buttons) == DialogResult.Yes)
					{
						this.SaveXml();
					}
					this.isEdit = false;
				}
				base.Close();
			}
			return false;
		}

		private void dgvLoadCal_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
		{
			if (!this.isSizeChanged)
			{
				this.percents.Clear();
				int width = this.dgvLoadCal.Width;
				for (int i = 0; i < this.dgvLoadCal.ColumnCount; i++)
				{
					this.percents.Add((double)this.dgvLoadCal.Columns[i].Width / (double)width);
				}
			}
		}

		private ExternalCommandData commandData;

		private const string Phase_A = "A";

		private const string Phase_B = "B";

		private const string Phase_C = "C";

		private const string Phase_3P = "A,B,C";

		private bool isFirst;

		private bool isProjectAdd;

		private bool isEdit;

		private bool isShow = true;

		private string fullPath = "";

		private object editPowerFactor;

		private int editRowIndex = -1;

		private int editColIndex = -1;

		private List<TransformerData> transformerDatas = new List<TransformerData>();

		private List<string> dgvRowValue = new List<string>();

		private List<double> percents = new List<double>();

		private bool isSizeChanged;
	}
}
